<?php
require_once("database.php");
class User extends Database
{
	function saveUser($data)
	{
		$pass=$data['password'];
	  	$data['password']=md5($pass);
		$data['history']=date('Y-m-d');
	  	$this->record=$data;
	  	$this->save('user_profile');
	  	echo mysql_insert_id();
	}
	function saveAddUser($data)
	{
		$pass=$data['password'];
	  	$data['password']=md5($pass);
		$data['history']=date('Y-m-d');
	  	$this->record=$data;
		$this->record['accept']=1;
	  	$this->save('user_profile');
	  	echo mysql_insert_id();
	}
	function dispMsgStatus($msg)
	{
		if($msg==0)
		{
			return 'Pending';
		}
		else if($msg==1)
		{
			return 'Accepted';
		}
		else if($msg==2)
		{
			return 'Rejected';
		}
	}
	function getPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pt from points WHERE points.profileid='$id' AND points.status='1' UNION ALL select pts from quiz_history WHERE quiz_history.profile_id='$id'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	function getQuizPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pts from quiz_history WHERE quiz_history.profile_id='$id'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	function getAcadSemPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pt from points WHERE points.profileid='$id' AND points.status='1' AND type='11'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	function getAcadInterPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pt from points WHERE points.profileid='$id' AND points.status='1' AND type='12'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	function getAttenPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pt from points WHERE points.profileid='$id' AND points.status='1' AND type='31'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	function getExtraInterPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pt from points WHERE points.profileid='$id' AND points.status='1' AND type='22'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	
	function getExtraIntraPoints($id)
	{
		$qpt=0;
		$res=$this->query("select pt from points WHERE points.profileid='$id' AND points.status='1' AND type='21'");
		while($row=mysql_fetch_array($res)){
	$qpt+=$row['pt'];
		}
		return $qpt;
	}
	function saveAddQuiz($data)
	{
		$data['history']=date('Y-m-d');
		$this->record=$data;
	  	$this->save('quiz_names');
	  	echo mysql_insert_id();
	}
	function saveAddQB($data)
	{
		$this->record=$data;
	  	$this->save('quiz_question_bank');
		$this->mkActive($data['test_id']);
	  	echo mysql_insert_id();
	}
	function mkActive($id)
	{
		$this->record['active']=1;
		$this->update('quiz_names',"quiz_id='$id'");
	}
	function getUserProfile($id)
	{
		$res=$this->query("select * from user_profile where id='$id'");
		$row=mysql_fetch_array($res);
		return $row;
	}
	function getHouse($id)
	{
		$res=$this->query("select house_name from house_details where house_id='$id'");
		$row=mysql_fetch_array($res);
		return $row['house_name'];
	}
	function getCommitee($id)
	{
		$res=$this->query("select commitee_name from commitee_details where commitee_id='$id'");
		$row=mysql_fetch_array($res);
		return $row['commitee_name'];
	}
	function getYear($id)
	{
		$res=$this->query("select year_name from year_details where year_id='$id'");
		$row=mysql_fetch_array($res);
		return $row['year_name'];
	}
	function getQuestion($id)
	{
		$res=$this->query("select no_of_ques from quiz_names where quiz_id='$id'");
		$row=mysql_fetch_array($res);
$limit_res=$this->query("select * from quiz_question_bank where test_id='$id' ORDER BY RAND() LIMIT 0,$row[no_of_ques]");
		return $limit_res;
	}
	function getProfileRequest()
	{
		$res=$this->query("select * from user_profile where accept=0 ORDER BY id ");
		return $res;
	}
	function getRole($role)
	{
		$res=$this->query("select role_name from user_role where role_id='$role'");
		$row=mysql_fetch_array($res);
		return $row['role_name'];
	}
	function updateQuizHistory($pro_id,$quiz_id,$result,$pt)
	{
		$count=$this->recCount('quiz_history',"profile_id='$pro_id' AND quiz_id='$quiz_id'");
		if($count>0)
		{
			$this->query("update quiz_history set result='$result' where profile_id='$pro_id' AND quiz_id='$quiz_id'");
			$this->query("update quiz_history set pts='$pt' where profile_id='$pro_id' AND quiz_id='$quiz_id'");
		}
		else
		{
			$this->record['profile_id']=$pro_id;
			$this->record['quiz_id']=$quiz_id;
			$this->record['result']=$result;
			$this->record['pts']=$pt;
			$this->record['date']=date('Y-m-d');
			$this->save('quiz_history');
		}
		$this->query("update quiz_history set status='1' where profile_id='$pro_id' AND quiz_id='$quiz_id'");
	}
	function editAddQuiz($data,$id)
	{
		$this->record=$data;
		$typ_res=$this->query("select quiz_type from quiz_names where quiz_id='$id' ");
		$typ_row=mysql_fetch_array($typ_res);
		if($typ_row['quiz_type']!=$data['quiz_type'])
		{
			$this->delete('quiz_question_bank',"test_id='$id'");	
		}
		$this->update('quiz_names',"quiz_id='$id'");
	}
	function updateAcadAttenPoints($id)
	{
		$pt_res=$this->query("select * from points where id='$id'");
		$pt_row=mysql_fetch_array($pt_res);
$limit_res=$this->query("select pts from point_limit where '$pt_row[special2]' between lower and higher AND type='$pt_row[type]' ORDER BY higher");
		$limit_row=mysql_fetch_array($limit_res);
		$this->record['pt']=$limit_row['pts'];
		$this->update("points","id='$id'");
	}
	function updateExtraPoints($id)
	{
		$pt_res=$this->query("select * from points where id='$id'");
		$pt_row=mysql_fetch_array($pt_res);
		$limit_res=$this->query("select pts from point_limit where '$pt_row[special3]' between lower and higher AND type='$pt_row[type]'");
		$limit_row=mysql_fetch_array($limit_res);
		$this->record['pt']=$limit_row['pts'];
		$this->update("points","id='$id'");
	}
}
?>